﻿using System;
using System.Threading;
using static System.Console;
using static System.Threading.Thread;

namespace Project_1
{
    /// <summary>
    /// 1.6 检测线程状态
    /// </summary>
    public static class Unit_1_06
    {
        /* CurretThread 获取当前线程
         * CurrentThread.ThreadState 获取当前线程的运行状态
         */
        public static void Go()
        {
            WriteLine("主线程开始");
            Thread t =new Thread(PrintNumbersWithStatus);
            Thread t2 = new Thread(DoNothing);
            WriteLine(t.ThreadState.ToString());
            t2.Start();
            t.Start();
            for (int i = 1; i < 30; i++)
            {
                WriteLine(t.ThreadState.ToString());
            }
            Sleep(TimeSpan.FromSeconds(6));
            WriteLine("线程停止");
            WriteLine(t.ThreadState.ToString());
            WriteLine(t2.ThreadState.ToString());
        }
        static void DoNothing()
        {
            Sleep(TimeSpan.FromSeconds(2));
        }

        static void PrintNumbersWithStatus()
        {
            WriteLine(".....");
            WriteLine(CurrentThread.Name);
            WriteLine(CurrentThread.ThreadState.ToString());
            for (int i = 1; i < 10; i++)
            {
                Sleep(TimeSpan.FromSeconds(2));
                WriteLine(i);
            }
        }
    }
}
